﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace MediaPlayer {
    /// <summary>
    /// 
    /// </summary>
    public class XmlHelper {
        private const string CAMERAS_COLLECTION = "Cameras";
        private const string IP = "Ip";
        private const string USERNAME = "Username";
        private const string PASSWORD = "Password";
        private const string CAMERA = "Camera";
        /// <summary>
        /// Prevents a default instance of the <see cref="XmlHelper"/> class from being created.
        /// </summary>
        private XmlHelper(){

        }

        /// <summary>
        /// Gets the cameras.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static CameraInfos GetCameras(string fileName){
            if (!File.Exists(fileName)) {
                return new CameraInfos();
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);
            CameraInfos infos = new CameraInfos();
            XmlNode root = doc.SelectSingleNode("//"+ CAMERAS_COLLECTION);
            if (root != null) {
                
                foreach (XmlNode node in root.ChildNodes){
                    CameraInfo info = new CameraInfo();
                    info.IP = node.Attributes[IP].Value;
                    info.Username = node.Attributes[USERNAME].Value;
                    info.Password = node.Attributes[PASSWORD].Value;
                    infos.Cameras.Add(info);
                }
            }
            return infos;
        }
        /// <summary>
        /// Saves the camera configs.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="cameraInfo">The camera info.</param>
        public static void SaveCameraConfigs(string fileName,CameraInfos cameraInfo){
            XmlDocument doc = new XmlDocument();

            if (File.Exists(fileName)) {
                doc.Load(fileName);
            }
            XmlNode root = doc.SelectSingleNode("//"+CAMERAS_COLLECTION);
            if (root == null) {
                root = doc.CreateElement(CAMERAS_COLLECTION);
                //var url = doc.CreateAttribute(WCF_URL);
                //url.Value = cameraInfo.WcfUrl;
                //root.AppendChild(url);
                doc.AppendChild(root);

            }
            root.RemoveAll();
            foreach (CameraInfo camera in cameraInfo.Cameras){
                XmlNode cameraNode = doc.CreateElement(CAMERA);
                var ip = doc.CreateAttribute(IP);
                var username = doc.CreateAttribute(USERNAME);
                var pass = doc.CreateAttribute(PASSWORD);
                ip.Value = camera.IP;
                username.Value = camera.Username;
                pass.Value = camera.Password;
                cameraNode.Attributes.Append(ip);
                cameraNode.Attributes.Append(username);
                cameraNode.Attributes.Append(pass);
                root.AppendChild(cameraNode);
            }
           
            doc.Save(fileName);
        }
    }
}
